# -*- coding: utf-8 -*-
import json

from  method import *
import unittest,random,time,uuid
# from ddt import ddt,data,unpack,file_data
# @ddt
class test_employee_entry():

    def ready_(self,data,base='test'):
        global name
        global phone
        global conn
        global cursor
        global base_sql
        if base=='test':
            conn = mysql('154')
        if base=='uat':
            conn = mysql('uat')
        cursor = conn.cursor()
        write_yaml(r'D:\python\pythonProject\test\add_employee_requests.yaml',data)
        timestamp = int(time.time())
        name = random.choice(xing) + random.choice(ming)
        # name = random.choice(xing) + random.choice(ming) + random.choice(zimu)
        phone = '188' + str(timestamp)[2:10]
        base_sql=base
        return name,phone,cursor

    #登录系统，取token
    def test_login(self):
        url = read_yaml(0,'登录')['url']
        headers = read_yaml(0,'登录')['headers']
        data=read_yaml(0,'登录')['data']
        expect=read_yaml(0,'登录')['expect']
        ree = send_requests(0,'登录', url, data, headers)
        a1 = json.loads(ree.text)
        # self.assertEqual(a1['status'], expect)
        token=a1['data']['token']
        return token

    #提交待入职信息、完成性格测评、背调
    def test_send_information(self):
        token=test_employee_entry().test_login()
        url = read_yaml(1,'提交待入职信息')['url']
        headers = read_yaml(1,'提交待入职信息')['headers']
        headers['Authorization']=token
        data=read_yaml(1,'提交待入职信息')['data']
        data['employeePhone']=phone
        data['employeeName']=name
        data['email']=phone +'@139.com'
        expect=read_yaml(1,'提交待入职信息')['expect']
        ree = send_requests(1, '提交待入职信息', url, data, headers)
        a1 = json.loads(ree.text)
        # self.assertEqual(a1['status'], expect)
        sql1="select id from entrant where employee_phone='%s';"%(phone)
        cursor.execute(sql1)
        print('员工姓名：'+name)
        result=cursor.fetchall()
        for i in result:
            global id
            id=i[0]
        #完成性格测评
        sql2="INSERT INTO employee_personality_assessment(`personality_assessment_id`, `activity_id`, `entrant_id`, `evaluation_progress`, `evaluation_results`, `answer_url`, `evaluation_results_url`, `effective_time`, `expiration_time`, `email`, `cc`, `title`, `content`, `operator_id`, `is_deleted`, `gmt_create`, `gmt_modify`) VALUES (NULL, 958685, %s, 15, 0, 'https://derkee.ceping.com/Login/Elink?elink=hyGLkE7s3E7WNgd2MFSFscxOCbaMzxcChina25EigVVYMdsBvmYivCCZq/nP2M/zmtg5wBYZ65rhy', '72176,72177', '2024-01-09 16:23:00', '2024-01-09 16:23:00', '', '', '', '', '007', 0, '2024-01-09 16:23:00', '2024-01-09 16:23:00');"
        param2 = (id,)
        cursor.execute(sql2,param2)
        conn.commit()
        #完成背调测评
        sql = "INSERT INTO emp_background_check (`background_id`, `entrant_id`, `back_status`, `risk_level`, `report_url`, `created_by`, `gmt_create`, `callback_time`, `cc`, `submit_time`, `number`, `completion_time`) VALUES (NULL, %s, 15, 4, 'https://lieni-file-test.oss-cn-hangzhou.aliyuncs.com/liechacha_common/20230609/10/c1f43f37-6d83-45b5-a390-d414d238abe8.pdf', '20280', '2024-01-17 15:03:11', '2024-01-17 15:03:11', '', '2024-01-17 15:03:11', '168846251718738121', 1688462519000);"
        param = (id,)
        cursor.execute(sql, param)
        conn.commit()
        #完成合同日期填补
        sql3="INSERT INTO employment_contract(`id`, `entrant_id`, `employee_id`, `contract_issue_date`, `start_date`, `stop_date`, `automatic_reminder`, `try_out_salary`, `positive_salary`, `operator_id`, `is_deleted`, `created_at`, `updated_at`, `deleted_at`, `template_serial`, `emp_contract_serial`, `emp_contract_status`, `operation_type`, `effective_contract`, `is_protocol`, `last_emp_contract_serial`, `emp_unit_price`) VALUES (NULL, %s, NULL, NULL, '2022-01-11', '2024-03-28', NULL, 9000.00, 10000.00, NULL, 0, '2024-01-15 14:48:56', '2024-01-15 15:01:38', NULL, '1800666d78134aaaaecf5d461fe81625', 'e3b7b082dc9f4c9e8bc0ec4992c95d73', 5, '10', NULL, NULL, '031faf67b9624aa8b17491acb6c50a2c', NULL);"
        param3=(id,)
        cursor.execute(sql3,param3)
        conn.commit()
        print('数据已经提交&数据库操作完毕！')
        #完成社保、公积金方案
        random_uuid = int(time.time())
        sql4="INSERT INTO empl_wage_scheme (`wage_scheme_id`, `employee_status`, `salary_calculate_way`, `com_unit_price`, `annual_bonus_type`, `city_of_social_type`, `city_of_social`, `ss_level_config_id`, `ss_pay_type`, `salary`, `gmt_create`, `gmt_modify`, `created_by`, `updated_by`, `is_deleted`, `entrant_id`, `work_province_code`, `work_city_code`, `work_area_code`,`hr_ss_pay_type`, `planned_admission_date`, `other_cost`, `other_cost_remark`) VALUES ('%s', 0, 1, 11111.00, 0, 1, 440300, 'SBGZ20240709005', 2, NULL, '2024-07-18 18:33:08', '2024-07-19 17:50:04', 'admin', 'admin', 0, %s, 110000, 110100, 110101,2, NULL, NULL, NULL);"%(random_uuid,id)
        cursor.execute(sql4)
        conn.commit()

        sql5="INSERT INTO empl_wage_scheme_detail (`wage_scheme_detail_id`, `wage_scheme_id`, `try_out_salary`, `positive_salary`, `social_security_base`, `provident_fund_base`, `entry_gross_profit`, `entry_gross_profit_margin`, `customer_minimum_gross_margin`, `ss_type`, `gmt_create`, `gmt_modify`, `created_by`, `updated_by`, `is_deleted`) VALUES (NULL, '%s', 111111.00, 111111111.00, 1111.00, 111111.00, -121171250.31, -1090552.16, 0.00, 1, '2024-07-19 17:50:04', '2024-07-19 17:50:04', 'admin', 'admin', 0);"%(random_uuid)
        cursor.execute(sql5)
        conn.commit()

        sql6="INSERT INTO empl_wage_scheme_detail (`wage_scheme_detail_id`, `wage_scheme_id`, `try_out_salary`, `positive_salary`, `social_security_base`, `provident_fund_base`, `entry_gross_profit`, `entry_gross_profit_margin`, `customer_minimum_gross_margin`, `ss_type`, `gmt_create`, `gmt_modify`, `created_by`, `updated_by`, `is_deleted`) VALUES (NULL, '%s', 20000.00, 20000.00, 2000.00, 2000.00, -80119.83, -721.09, 0.00, 2, '2024-07-19 17:50:04', '2024-07-19 17:50:04', 'admin', 'admin', 0);"%(random_uuid)
        cursor.execute(sql6)
        conn.commit()
    # 获取流程节点信息
    def audit_config(self,bizType,accountManagerId,clientId,attributionId,employeeId):
        token = test_employee_entry().test_login()
        url = read_yaml(25, '查询流程节点信息')['url']
        headers = read_yaml(25, '查询流程节点信息')['headers']
        headers['Authorization'] = token
        data = read_yaml(25, '查询流程节点信息')['data']
        data['bizType'] = bizType
        data['accountManagerId'] = accountManagerId
        data['clientId'] = clientId
        data['attributionId'] = attributionId
        data['employeeId']=employeeId
        ree = send_requests(25, '查询流程节点信息', url, data, headers)
        a1 = json.loads(ree.text)
        auditConfigVO = a1['data']
        return auditConfigVO

    # hhx提交入职信息（相当于小程序扫码入职）
    def test_scanningcode(self):
        token = test_employee_entry().test_login()
        url = read_yaml(3, '小程序扫码')['url']
        headers = read_yaml(3, '小程序扫码')['headers']
        headers['Authorization'] = token
        data = read_yaml(3, '小程序扫码')['data']
        data['entrantId'] = id
        data['employeeName'] = name
        data['contactWay'] = phone
        expect = read_yaml(3, '小程序扫码')['expect']
        ree = send_requests(3, '小程序扫码', url, data, headers)
        a1 = json.loads(ree.text)
        # self.assertEqual(a1['status'], expect)

        #补充近3年社保缴费记录和近3年公积金缴费记录
        sql = "UPDATE certificate_resources SET three_year_ss_payment_records='10006644',three_year_af_payment_records='10006630' WHERE entrant_id=%s"%(id)
        cursor.execute(sql)
        conn.commit()
        print('小程序扫码入职完毕！')

    # 发起入职申请流程
    def test_application_process(self):
        token = test_employee_entry().test_login()
        #查询流程节点信息
        auditConfigVO=test_employee_entry().audit_config(bizType='109',accountManagerId='20280',
                                                         attributionId=1,clientId=391,employeeId=None)

        #提交入职申请流程
        url = read_yaml(2, '入职申请')['url']
        headers = read_yaml(2, '入职申请')['headers']
        headers['Authorization'] = token
        data = read_yaml(2, '入职申请')['data']
        data['entrantId'] = id
        data['employeeName'] = name
        data['contactWay'] = phone
        data['auditConfigVO']=auditConfigVO
        expect = read_yaml(2, '入职申请')['expect']
        ree = send_requests(2, '入职申请', url, data, headers)
        a1 = json.loads(ree.text)
        # self.assertEqual(a1['status'], expect)
        print('入职流程申请完毕！')

    # 审批流程
    def test_approval_process(self):
        #查询流程编号
        sql4 = "SELECT flow_number FROM audit WHERE employee_name='%s' ORDER BY audit_id DESC LIMIT 1;"%(name)
        cursor.execute(sql4)
        conn.commit()
        result = cursor.fetchall()
        for i in result:
            flow_number = i[0]
        print('流程编号:' + str(flow_number))

        #调接口进行流程审批
        process_approval(flow_number=flow_number,environment=base_sql)

    # 生成合同
    def test_creat_contract(self):
        #查询员工工号
        sql7 = "SELECT employee_id FROM employee_info WHERE contact_way ='%s'" % (phone)
        cursor.execute(sql7)
        conn.commit()
        result = cursor.fetchall()
        for i in result:
            global employee_id
            employee_id = i[0]
        print('员工工号是：'+str(employee_id))

        #更新员工密码
        sql8 = "UPDATE employee_info SET password='$2a$10$0lbPGdjNzTK40DHiRtGiU.ekDMIZ16flo0ncQqaVUrzJ2vuNfLKYm' WHERE employee_id='%s'" % (employee_id)
        cursor.execute(sql8)
        conn.commit()
        token = test_employee_entry().test_login()
        url = read_yaml(6, '生成合同')['url']
        headers = read_yaml(6, '生成合同')['headers']
        headers['Authorization']=token
        expect= read_yaml(6, '生成合同')['expect']
        data = read_yaml(6, '生成合同')['data']
        data[0]['employeeId']=employee_id
        data[0]['entrantId']=id
        data[0]['employeeName']=name
        ree1 = send_requests(6, '生成合同', url, data, headers)
        a1 = json.loads(ree1.text)
        # self.assertEqual(a1['status'], expect)
        print('员工合同生成成功！')

    # 推送合同到小程序
    def test_send_contract(self):
        token = test_employee_entry().test_login()
        url = read_yaml(7, '推送合同')['url']
        headers = read_yaml(7, '推送合同')['headers']
        headers['Authorization'] = token
        expect = read_yaml(7, '推送合同')['expect']
        data = read_yaml(7, '推送合同')['data']
        data[0]['employeeId'] = employee_id
        data[0]['entrantId'] = id
        data[0]['employeeName'] = name
        ree1 = send_requests(7, '推送合同', url, data, headers)
        a1 = json.loads(ree1.text)
        # self.assertEqual(a1['status'], expect)
        print('员工合同推送成功！')
    #员工确认到岗
    def test_confirm_duty(self):
        token = test_employee_entry().test_login()
        url = read_yaml(8, '确认到岗')['url']
        headers = read_yaml(8, '确认到岗')['headers']
        headers['Authorization'] = token
        expect = read_yaml(8, '确认到岗')['expect']
        data = read_yaml(8, '确认到岗')['data']
        data['employeeId'] = employee_id
        ree1 = send_requests(8, '确认到岗', url, data, headers)
        a1 = json.loads(ree1.text)
        # self.assertEqual(a1['status'], expect)
        print('员工确认到岗成功！')
    #员工入场、发起员工计费流程
    def test_GoAdmission(self):
        token = test_employee_entry().test_login()
        # 查询流程节点信息
        auditConfig = test_employee_entry().audit_config(bizType=30,accountManagerId='20280',
                                                         attributionId=1,clientId=391,employeeId=employee_id)
        # 查询新入职待派遣员工ID
        sql = "SELECT dispatched_staff_id FROM dispatched_staff WHERE employee_id ='%s';" % (employee_id)
        cursor.execute(sql)
        conn.commit()
        result = cursor.fetchall()
        for i in result:
            dispatched_staff_id = i[0]

        url = read_yaml(10, '发起计费流程')['url']
        headers = read_yaml(10, '发起计费流程')['headers']
        headers['Authorization'] = token
        expect = read_yaml(10, '发起计费流程')['expect']
        data = read_yaml(10, '发起计费流程')['data']
        data['employeeId'] = employee_id
        data['employeeName']=name
        data['dispatchedStaffId']=dispatched_staff_id
        data['auditConfig']=auditConfig
        ree1 = send_requests(10, '发起计费流程', url, data, headers)
        a1 = json.loads(ree1.text)
        print('发起员工计费流程成功！')
        # 查询流程编号
        sql5 = "SELECT flow_number FROM  audit  WHERE  audit_status=0 AND employee_id='%s';"%(employee_id)
        # print(employee_id)
        cursor.execute(sql5)
        conn.commit()
        result = cursor.fetchall()
        for i in result:
            flow_number = i[0]
        print('流程编号:' + str(flow_number))
        # 调接口进行流程审批
        process_approval(flow_number=flow_number,environment=base_sql)



    def test_billing_process(self):
        print('自动化操作部分已经执行完毕,请去小程序完成合同签订、PC端法人盖章等操作，后续操作敬请期待...')


if __name__=="__main__":
    for i in range(1):

        start_time = time.time()   # 记录开始时间
        # 这里写需要测试运行时间的代码
        test_employee_entry().ready_(base='test',data=data1)
        # test_employee_entry().ready_(base='uat', data=data_uat1)
        test_employee_entry().test_send_information()
        test_employee_entry().test_scanningcode()
        test_employee_entry().test_application_process()
        test_employee_entry().test_approval_process()
        test_employee_entry().test_creat_contract()
        test_employee_entry().test_send_contract()
        test_employee_entry().test_confirm_duty()
        # test_employee_entry().test_GoAdmission()
        test_employee_entry().test_billing_process()
        end_time = time.time()   # 记录结束时间
        run_time = end_time - start_time    # 计算运行时间（单位为秒）
        print("脚本运行时间：", round(run_time, 2), '秒')
















